﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;

namespace KylinORM.Data.SqlServer
{
    internal sealed class SqlServerHelper
    {
        public static string connectionString { get; set; }

        private static int _commandTimeout = 300;
        public static int commandTimeout
        {
            get { return _commandTimeout; }
            set { _commandTimeout = value; }
        }

        #region private methods

        private static T execute<T>(IDbCommand cmd, Func<IDbCommand, T> action)
        {
            IDbConnection connection = cmd.Connection;
            if (connection.State == ConnectionState.Closed)
            {
                using (connection)
                {
                    connection.Open();
                    return action(cmd);
                }
            }
            else
            {
                return action(cmd);
            }
        }

        #endregion

        #region ExecuteDataSet

        /// <summary>
        /// 执行一条SQL语句，返回一个数据集
        /// </summary>
        /// <param name="cmd">需要执行的语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(IDbCommand cmd)
        {
            return execute(cmd, d => 
            {
                IDataAdapter da = new SqlDataAdapter(d as SqlCommand);
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            });
        }
        #endregion

        #region ExecuteDataTable
        /// <summary>
        /// 执行一条SQL语句，返回一个数据表
        /// </summary>
        /// <param name="cmd">需要执行的语句</param>
        /// <returns></returns>
        public static DataTable ExecuteDataTable(IDbCommand cmd)
        {
            return ExecuteDataSet(cmd).Tables[0];
        }
        #endregion

        #region ExecuteDataRow
        /// <summary>
        /// 执行一条SQL语句，返回一个数据行
        /// </summary>
        /// <param name="cmd">需要执行的语句</param>
        /// <returns></returns>
        public static DataRow ExecuteDataRow(IDbCommand cmd)
        {
            DataTable dt = ExecuteDataTable(cmd);
            return dt.Rows.Count == 0 ? null : dt.Rows[0];
        }
        #endregion

        #region ExecuteScalar

        /// <summary>
        /// 执行一条SQL语句，返回第一行第一列的值
        /// </summary>
        /// <param name="cmd">需要执行的语句</param>
        /// <returns></returns>
        public static object ExecuteScalar(IDbCommand cmd)
        {
            return execute(cmd, d => d.ExecuteScalar());
        }

        #endregion

        #region ExecuteNonQuery

        /// <summary>
        /// 执行一条SQL语句，返回语句所影响的行数
        /// </summary>
        /// <param name="cmd">需要执行的语句</param>
        /// <returns></returns>
        public static int ExecuteNonQuery(IDbCommand cmd)
        {
            return execute(cmd, d => d.ExecuteNonQuery());
        }
        #endregion

    }

}
